<template>
  <a-modal title="客户管理" :width="800" :visible="visible" :confirmLoading="confirmLoading" :maskClosable="false" @ok="handleSubmit"
           @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">

      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="代理商名称">
          <a-select showSearch optionFilterProp="children" :filterOption="filterOption"
                    v-decorator="['parentCode', {rules: [{required: true, message: '必输'}]}]">
            <a-select-option v-for="item in companys" :key="item.code">{{item.name}}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="简称">
          <a-input v-decorator="['name', {rules: [{required: true, message: '必输'}]}]"></a-input>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="全称">
          <a-input v-decorator="['fullName', {rules: [{required: true, message: '必输'}]}]"></a-input>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="微信商户号">
          <a-input  v-decorator="[ 'mchId', {rules: [{ required: true, message: '请输入。。。' }] }]"/>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="商户秘钥">
          <a-input v-decorator="['mchKey', {rules: []}]"></a-input>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="appid">
          <a-input v-decorator="['appid', {rules: []}]"></a-input>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="状态">
          <a-radio-group buttonStyle="solid"
                         v-decorator="[ 'status', {rules: [{ required: true, message: '请选择' }], initialValue: true}]">
            <a-radio-button :value="true">启用</a-radio-button>
            <a-radio-button :value="false">禁用</a-radio-button>
          </a-radio-group>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="类型">
          <a-radio-group buttonStyle="solid"
                         v-decorator="[ 'type', {rules: [{ required: true, message: '请选择' }], initialValue: 1}]">
            <a-radio-button :value="1">未满30天</a-radio-button>
            <a-radio-button :value="2">条码支付</a-radio-button>
            <a-radio-button :value="3">H5支付</a-radio-button>
            <a-radio-button :value="4">网银充值</a-radio-button>
            <a-radio-button :value="5">代付</a-radio-button>
            <a-radio-button :value="6">满30天未开通</a-radio-button>
          </a-radio-group>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="最大收款金额">
          <a-input addonAfter="元" v-decorator="['maxAmount', {rules: []}]"></a-input>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="最大收款次数">
          <a-input v-decorator="['maxCount', {rules: []}]"></a-input>
        </a-form-item>
        <!--<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="费率">
          <a-input addonAfter="3位小数" v-decorator="['rate', {rules: []}]"/>&nbsp;
        </a-form-item>-->
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="代付金额">
          <a-input addonAfter="元" v-decorator="['amount', {rules: []}]"></a-input>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="超管">
          <a-select v-decorator="[ 'adminStr', {rules: []} ]">
            <a-select-option key="8812">8812</a-select-option>
            <a-select-option key="1124">1124</a-select-option>
            <a-select-option key="9708">9708</a-select-option>
            <a-select-option key="9600">9600</a-select-option>
            <a-select-option key="5260">5260</a-select-option>
            <a-select-option key="小南">小南</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>

    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import { saveOrUpdate } from '@/api/shop/storetest'
  /**
   * 数据
   */
  const data = function() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      id: null,
      visible: false,
      confirmLoading: false,
      isDisabled: false,
      form: this.$form.createForm(this)
    }
  }

  const created = function() {

  }

  export default {
    props: {
      companys: {
        type: Array,
        default: null
      }
    },
    data: data,
    created: created,
    /**
     * 方法
     */
    methods: {
      add () {
        this.visible = true
      },
      edit (record) {
        this.visible = true
        this.id = record.id
        this.isDisabled = true
        const { form: { setFieldsValue } } = this
        this.$nextTick(() => {
          const formData = pick(record, ['parentCode','name', 'fullName', 'appid', 'mchKey', 'mchId', 'status', 'type',
          'maxAmount', 'maxCount', 'rate', 'amount', 'adminStr'])
          setFieldsValue(formData)
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            values.id = this.id
            saveOrUpdate(values).then((res) => {
              this.confirmLoading = false
              if (res.code === 200) {
                this.handleCancel()
                this.$emit('ok')
              }
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.id = null
        this.visible = false
        this.isDisabled = false
        this.confirmLoading = false
        this.form.resetFields()
      },
      filterOption (input, option) {
        return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      },

    },
  }
</script>